/**
 * 
 */
package com.qq.spirit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.Header;

import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.qq.spirit.DinnerTask.DinnerTeamAction;
import com.qq.spirit.inter.AbstractActionAfterHttpRequest;
import com.qq.spirit.inter.IActionAfterHttpRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

/**
 * loyalty 民忠 forceimposecost 强制征收所需金币 imposenum 征收次数 cdflag 征收时间 征收任务
 * 
 * @author beedoorwei
 * 
 */
public class ImposeTask extends TaskUnit
{
	@Override
	public void doTask()
	{

		Iterator<String> ite = UserCache.userQueue.keySet().iterator();
		while (ite.hasNext())
		{
			String username = ite.next();
			TaskManager.addTask(new Impose(username));
		}
	}

}

/**
 * 具体征收任务
 * 
 * @author Administrator
 * 
 */
class Impose extends TaskUnit
{
	public Impose(String username)
	{
		super();
		this.username = username;
	}

	@Override
	public void doTask()
	{
		try
		{
			UserData u = UserCache.get(username);
			if (u.getUserState() == UserState.LOGIN_GAME)
			{
				HttpUtil.doGet(username, GameConstants.IMPOSE_PRE_URL, new ImposePreTaskAction(username));
			} else
			{
				// 添加登录任务先
				TaskManager.addTask(new LoginTask(username, this));
			}
		} catch (Exception e)
		{
			LoggerUtil.error(e);
		}
	}
}

/**
 * 
 * 征收前事件查询
 * 
 * @author Administrator
 * 
 */
class ImposePreTaskAction extends AbstractActionAfterHttpRequest
{
	public ImposePreTaskAction(String username)
	{
		super();
		this.username = username;
		this.taskUnit = new Impose(username);
	}

	public void doCall(List<NameValuePair> headerList, XmlNodeData imposeInfo)
	{
		UserData u = UserCache.get(username);
		// 判断能否征收
		String imposenum = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.imposenum");
		String cdflag = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.cdflag");
		String lastimposetime = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.lastimposetime");
		if (GameUtil.convertInt(imposenum) <= 0)
		{
			return;
		}
		if (cdflag.equals("1") && GameUtil.convertInt(lastimposetime) >0)
		{
			LoggerUtil.info(UserCache.getGameName(username) + "征收cd中,休眠：" + GameUtil.getRemainTime(lastimposetime) + "\t 后执行");
			TaskManager.addTask(new Impose(username), GameUtil.convertInt(lastimposetime) / 1000 + 1);
			return;
		}
		u.setImposeInfo(imposeInfo);
		// 首先查看是否有征收事件
		doImposeAction(imposeInfo);
		// 查看是否有征收次数
		String imposeNumStr = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.imposenum");
		// 查到数据，看看值是多少
		int imposeNum = GameUtil.convertInt(imposeNumStr);
		if (imposeNum > 0)
		{
			// 有征收次数，开始征收吧
			HttpUtil.doGet(username, GameConstants.IMPOSE_DO_URL, new ImposeDoTaskAction(username));
		} else
		{
			// 如果征收次数为0，那么查看强征次数是否小于设定的值
			String forceImposeNumStr = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.forceimposecost");
			// 查到数据，看看值是多少
			int forceImposeNum = GameUtil.convertInt(forceImposeNumStr);
			if (forceImposeNum <= 5)
			{
				// 强征次数小于指定值，开始强征吧
				HttpUtil.doGet(username, GameConstants.IMPOSE_FORCE_DO_URL, new ImposeDoTaskAction(username));
			}
		}
	}

	/**
	 * 征收事件处理
	 */
	private void doImposeAction(XmlNodeData imposeInfo)
	{
		// 首先判断是否有征收事件
		List<XmlNodeData> larrydtoList = XmlDataUtil.getNodeInfoList(imposeInfo, "results.larrydto");
		if (!larrydtoList.isEmpty())
		{
			// 有征收事件，先做征收事件吧。判断民忠，是否低于90
			String loyaltyStr = XmlDataUtil.getNodeData(imposeInfo, "results.imposedto.loyalty");
			int loyalty = GameUtil.convertInt(loyaltyStr);
			if (loyalty < 90)
			{
				// 判断事件名称
				String larrydName = XmlDataUtil.getNodeData(imposeInfo, "results.larrydto.name");
				// 判断事件是否是加金币的
				String opt = GameConstants.imposeEventMap.get(larrydName);
				List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
				nvpList.add(new NameValuePair("opt", opt));
				HttpUtil.doPost(username, GameConstants.IMPOSE_SELECT_URL, nvpList, new ImposeActionSelectTask(username));
			}

		}
	}

}

/**
 * 征收事件选择
 * 
 * @author Administrator
 * 
 */
class ImposeActionSelectTask extends AbstractActionAfterHttpRequest
{

	public ImposeActionSelectTask(String username)
	{
		super();
		this.username = username;
		this.taskUnit = new Impose(username);
	}

	@Override
	public void doCall(List<NameValuePair> headerList, XmlNodeData imposeInfo)
	{
		TaskManager.addTask(new Impose(username), GameUtil.randomInt(10));
	}
}

/**
 * 执行征收
 * 
 * @author Administrator
 * 
 */
class ImposeDoTaskAction extends AbstractActionAfterHttpRequest
{

	public ImposeDoTaskAction(String username)
	{
		super();
		this.username = username;
		this.taskUnit = new Impose(username);
	}

	public void doCall(List<NameValuePair> headerList, XmlNodeData imposeInfo)
	{
		UserData u = UserCache.get(username);
		u.setImposeInfo(imposeInfo);
		String cooperdis = XmlDataUtil.getNodeData(imposeInfo, "results.cooperdis");
		String golddis = XmlDataUtil.getNodeData(imposeInfo, "results.golddis");
		LoggerUtil.info(UserCache.getGameName(username) + "征收获得银币：" + cooperdis + "\t 金币：" + golddis);

		// 判断是否还征收次数，如果有，那么提交一个新的，在征收cd结束后开始的征收任务
		String imposenum = XmlDataUtil.getNodeData(imposeInfo, "results.imposenum");
		if (GameUtil.convertInt(imposenum) > 0)
		{
			String imposecd = XmlDataUtil.getNodeData(imposeInfo, "results.imposecd");
			// 提交征收任务，计算下次征收时间
			TaskManager.addTask(new Impose(username), GameUtil.convertInt(imposecd) / 1000);
		}
	}
}
